<!--/**
* @author xuyh0817
* @email xuyh0817@foxmail.com
* @date 2022-02-16 15:58
* @version 2.0.0
* @doc todo
* @website todo
* @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
*/
-->
<template>
  <a-cascader
      v-bind="attrs"
      :value="currentValue"
      :options="options"
      :field-names="fieldNames"
      :show-search="showSearch"
      @change="changeHandler"
      @search="searchHandler"
  >
<!--    <extend-slot v-for="(render, key) of slots" v-slot:[key] :key="key" :data="slotData" :render="render"/>-->
  </a-cascader>
</template>

<script>

import ComponentMixin from '../../XRenderMixins/XRenderComponentMixin'
import {COMPONENT_TYPE, CUSTOM_EVENT_TYPE} from '../../constants'

export default {
  name: COMPONENT_TYPE.CASCADER,
  mixins: [ComponentMixin],
  props: {
    value: {
      type: [String, Array]
    }
  },
  computed: {
    currentValue: {
      get() {
        const {value, formItem, isDebug} = this
        if (!(value instanceof Array) && typeof (value) !== 'string') {
          isDebug ? console.warn(`${formItem.prop} value类型错误,非String或Array`) : ''
          return []
        }
        if (value instanceof Array) {
          return value || []
        }
        return value.split(',')
      }
    },
    options: {
      get() {
        const {attrs, formItem, isDebug} = this
        const {options} = attrs
        if (!(options instanceof Array) && typeof (options) !== 'string') {
          isDebug ? console.warn(`${formItem.prop} options类型错误,非String或Array`) : ''
          return []
        }
        if (options instanceof Array) {
          return options || []
        }
        return (new Function('return ' + options))()
      }
    },
    fieldNames: {
      get() {
        const {attrs, formItem, isDebug} = this
        const {fieldNames} = attrs
        if (typeof (fieldNames) !== 'object' && typeof (fieldNames) !== 'string') {
          isDebug ? console.warn(`${formItem.prop} fieldNames类型错误,非String或Object`) : ''
          return {label: 'label', value: 'value', children: 'children'}
        }
        if (typeof (fieldNames) === 'object') {
          return fieldNames
        }
        return (new Function('return ' + fieldNames))()
      }
    },
    showSearch: {
      get() {
        const {attrs} = this
        const {isShowSearch, showSearchLimit} = attrs
        return isShowSearch ? {filter: this.searchFilter, limit: showSearchLimit || 50} : false
      }
    }
  },
  methods: {
    searchHandler(value) {
      const {attrs, formItem} = this
      this.$emit(CUSTOM_EVENT_TYPE.CUSTOM_EVENT, {
        type: CUSTOM_EVENT_TYPE.SEARCH_EVENT,
        compType: COMPONENT_TYPE.CASCADER,
        prop: formItem ? formItem.prop : '',
        value,
        attrs: attrs,
        formItem: formItem
      })
    },
    changeHandler(value) {
      const {attrs, formItem, isDebug, options} = this
      const obj = {}
      // 关联的key
      let {associations} = attrs

      if (typeof (associations) !== 'string' && !(associations instanceof Array)) {
        isDebug ? console.warn(`${formItem.prop} associations类型错误,非String或Array`) : ''
        associations = []
      }
      if (typeof (associations) === 'string') {
        associations = (new Function('return ' + associations))()
      }
      if (associations instanceof Array) {
        associations = associations || []
      }

      for (const val of value) {
        // 关联的key
        for (const option of options) {
          if (option.value === val) {
            for (const association of associations) {
              if (!!association.key && !!association.mapKey) {
                obj[association.key] = option[association.mapKey]
              }
            }
          }
        }
      }

      this.$emit(CUSTOM_EVENT_TYPE.CUSTOM_EVENT, {
        type: CUSTOM_EVENT_TYPE.ASSOCIATION_CHANGE_EVENT,
        compType: COMPONENT_TYPE.CASCADER,
        prop: formItem ? formItem.prop : '',
        value: obj,
        attrs: attrs,
        formItem: formItem
      })

      const valueStr = value.join(',')
      this.$emit(CUSTOM_EVENT_TYPE.CHANGE, {
        type: COMPONENT_TYPE.CASCADER,
        prop: formItem ? formItem.prop : '',
        value: valueStr,
        attrs: attrs,
        formItem: formItem
      })
    },
    searchFilter(inputValue, path) {
      return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1)
    }
  }
}
</script>

<style scoped>

</style>
